<template>
  <el-dialog
        title=""
        :visible.sync="showDialog"
        center
        :custom-class="'successCode-dialog'"
        :modal="false"
        width="520px"
        :close-on-click-modal="false"
        @close="handleClose"
        >
    <div class="login">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" v-if="verified">
        <h3 class="title">登录/注册</h3>
        <el-form-item prop="phonenumber">
          <el-input
            v-model="loginForm.phonenumber"
            type="text"
            auto-complete="off"
            placeholder="账号"
          >
          </el-input>
        </el-form-item>
        <el-form-item prop="smsCode" class="form-item-code">
          <el-input
            v-model="loginForm.smsCode"
            auto-complete="off"
            placeholder="验证码"
            @keyup.enter.native="handleLogin"
          >
          </el-input>
          <el-button
            size="medium"
            type="primary"
            :disabled="messCode != '' && timeCount != 0"
            @click.native.prevent="handleGetCode()"
            class="send-code"
          >
          {{timeCount == 30?'发送验证码':timeCount == 0?'重新发送':timeCount}}
          </el-button>
        </el-form-item>
        <el-form-item style="width:100%;">
          <el-button
            :loading="loading"
            size="medium"
            type="primary"
            style="width:100%;"
            @click.native.prevent="handleLogin"
          >
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
        </el-form-item>
      </el-form>

      <slide-verify
        :l="42"
        :r="10"
        :w="350"
        :h="155"
        slider-text="向右滑动"
        @success="onSuccess"
        @fail="onFail"
        v-else
      />
    </div>
  </el-dialog>
</template>

<script>
import { sendCode } from "@/api/login";

export default {
  name: "LoginForm",
  data() {
    return {
      showDialog:false,
      verified:true,
      loginForm: {
        phonenumber: "",
        smsCode: ""
      },
      loginRules: {
        phonenumber: [
          { required: true, trigger: "blur",validator: this.validatePhone }
        ],
        smsCode: [
          { required: true, trigger: "blur", message: "请输入您的验证码" }
        ],
      },
      loading: false,
      messCode:'',//验证码
      timeCount:30,
      interval:null,//计时器
    };
  },
  watch: {
    "$store.state.user.loginStatus":{
      handler:function(newVal,oldVal){
        this.showDialog = newVal;
        if(newVal){
          this.verified = true;
        }
      },
      deep:true,
    }
  },
  created() {
  },
  methods: {
     //手机号校验
     validatePhone(rule, value, callback){
      console.log(6666688888);
      console.log(value);
      if (value == '') {
        callback(new Error('请输入您的手机号'));
      } else {
        var reg =/^1[3456789]\d{9}$/;
        if(!reg.test(value)){
          callback(new Error('请输入正确格式的手机号'));
        }
        callback();
      }
    },
    //获取验证码
    handleGetCode(){
      this.$refs['loginForm'].validateField('phonenumber', valid => {
        if (!valid) {
          // 发送验证码
          sendCode({phonenumber:this.loginForm.phonenumber}).then(res => {
            if(res.code == 200){
              this.messCode = res.data;
              this.timeCount = 30;
              this.interval = setInterval(()=>{
                if(this.timeCount > 0){
                  this.timeCount --;
                }
              },1000)
            }
          });
        }
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          //登录成功，打开滚动验证
          this.verified = false;
        }
      });
    },
    loginSubmit(){
      this.loading = true;
      this.$store.dispatch("Login", this.loginForm).then(() => {
        this.$store.dispatch('GetInfo').then(() => {
          this.showDialog = false;
        })
      }).catch(() => {
        this.loading = false;
      });
    },
    handleClose(){
      this.$store.dispatch("SetLoginStatus", false)
      this.verified = false;
    },
    onSuccess(){
      this.verified = true;
      this.loginSubmit();
    },
    onFail(){

    }
  }
};
</script>

<style lang="scss" scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}
.form-item-code{
  margin-bottom:20px!important;
  ::v-deep .el-form-item__content{
    display: flex;
    align-items: center;
  }
  .send-code{
    margin-left: 10px;
  }
}
.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 38px;
}
</style>
